import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { environment } from 'src/environments/environment';
@Injectable({
  providedIn: 'root'
})
export class StorehouseService {

  constructor(private http: HttpClient) { }
  private createPaginationParams(currentPage: number, itemsPerPage: number): HttpParams {
    return new HttpParams()
      .set('page', currentPage.toString())
      .set('pageSize', itemsPerPage.toString());
  }// 创建分页参数

  getWarehouseCategorysWithChildren(page?: number, pageSize?: number, keyword?: string) {
    let params = new HttpParams();
    if (page !== undefined && pageSize !== undefined) {
      params = this.createPaginationParams(page, pageSize);
    }
    if (keyword !== undefined) {
      params = params.set('keyword', keyword);
    }
    return this.http.get(`${environment.apiUrl}/Warehouse/GetPagedWarehouseCategorysWithChildren`, {
      params: params
    })
  }

  createWarehouseCategory(data: any) {
    return this.http.post(`${environment.apiUrl}/Warehouse/CreateWarehouseCategory`, data)
  }

  updateWarehouseCategory(data: any, id: number) {

    return this.http.put(`${environment.apiUrl}/Warehouse/UpdateWarehouseCategory?id=${id}`, data)
  }
  deleteWarehouseCategory(id: number) {
    return this.http.delete(`${environment.apiUrl}/Warehouse/DeleteWarehouseCategory`, {
      params: { id: id }
    })
  }

  downloadWarehouseCategoryFileModel() {
    return this.http.get(`${environment.apiUrl}/Excel/DownloadWarehouseCategoryFileModel`, { responseType: 'blob' })
  }// 下载仓库分类模板
  importWarehouseCategoriesFromExcel(file: File) {
    const formData = new FormData();
    formData.append('file', file);
    const url = `${environment.apiUrl}/Excel/importWarehouseCategoriesFromExcel`;
    return this.http.post(url, formData, { responseType: 'text' });
  }// 导入仓库分类
  getWarehousePositionsByWarehouseId(warehouseId: number, page?: number, pageSize?: number) {
    let params = new HttpParams().set('warehouseId', warehouseId.toString());
    if (page !== undefined && pageSize !== undefined) {
      params = params.append('page', page.toString()).append('pageSize', pageSize.toString());
    }
    return this.http.get(`${environment.apiUrl}/Warehouse/GetPagedAllPositionsByWarehouseId`, {
      params: params
    })
  }// 根据仓库id获取库位

  getAllWarehouses() {
    return this.http.get(`${environment.apiUrl}/Warehouse/GetAllWarehouses`)
  }// 获取所有仓库

  getWarehouses(WarehouseCategoryId?: number, Status?: string, SearchTerm?: string, PageNumber?: number, PageSize?: number) {
    let params = new HttpParams();
    if (WarehouseCategoryId) {
      params = params.set('WarehouseCategoryId', WarehouseCategoryId.toString());
    }
    if (Status) {
      params = params.set('Status', Status.toString());
    }
    if (SearchTerm) {
      params = params.set('SearchTerm', SearchTerm);
    }
    if (PageNumber) {
      params = params.set('PageNumber', PageNumber.toString());
    }
    if (PageSize) {
      params = params.set('PageSize', PageSize.toString());
    }
    return this.http.get(`${environment.apiUrl}/Warehouse/GetWarehouses`, {
      params: params
    })
  }// 获取仓库


  CreateWarehouse(data: any) {
    return this.http.post(`${environment.apiUrl}/Warehouse/CreateWarehouse`, data)
  }// 创建仓库

  deleteWarehouse(id: number) {
    return this.http.delete(`${environment.apiUrl}/Warehouse/DeleteWarehouse`, {
      params: { id: id }
    })
  }// 删除仓库

  disableWarehouses(data: any) {
    return this.http.put(`${environment.apiUrl}/Warehouse/DisableWarehouses`, data)
  }// 禁用仓库

  enableWarehouses(data: any) {
    return this.http.put(`${environment.apiUrl}/Warehouse/EnableWarehouses`, data)
  }// 启用仓库

  getWarehouseById(id: number) {
    return this.http.get(`${environment.apiUrl}/Warehouse/GetWarehouseById`, {
      params: { id: id }
    })
  }// 根据id获取仓库

  updateWarehouse(id: any, data: any) {
    return this.http.put(`${environment.apiUrl}/Warehouse/UpdateWarehouse`, data, { params: { id } })
  }// 修改仓库

  deleteWarehouses(id: any) {
    const options = {
      body: id,
      responseType: 'text' as 'json'
    };
    return this.http.delete(`${environment.apiUrl}/Warehouse/DeleteWarehouses`, options)
  }// 批量删除仓库

  downloadWarehouseFileModel() {
    return this.http.get(`${environment.apiUrl}/Excel/DownloadWarehouseFileModel`, { responseType: 'blob' })
  }// 下载仓库模板


  importWarehousesFromExcel(file: File) {
    const formData = new FormData();
    formData.append('file', file);
    const url = `${environment.apiUrl}/Excel/ImportWarehousesFromExcel`;
    return this.http.post(url, formData, { responseType: 'text' });
  }// 导入仓库


  downloadWarehouseFile() {
    return this.http.get(`${environment.apiUrl}/Excel/DownloadWarehouseFile`, { responseType: 'blob' })
  }// 下载仓库

  getAllPositions() {
    return this.http.get(`${environment.apiUrl}/Warehouse/GetAllPositions`)
  }// 获取所有库位

  getPositions(WarehouseId?: number, Status?: string, SearchTerm?: string, PageNumber?: number, PageSize?: number) {
    let params = new HttpParams();
    if (WarehouseId) {
      params = params.set('WarehouseId', WarehouseId.toString());
    }
    if (Status) {
      params = params.set('Status', Status.toString());
    }
    if (SearchTerm) {
      params = params.set('SearchTerm', SearchTerm);
    }
    if (PageNumber) {
      params = params.set('PageNumber', PageNumber.toString());
    }
    if (PageSize) {
      params = params.set('PageSize', PageSize.toString());
    }
    return this.http.get(`${environment.apiUrl}/Warehouse/GetPositions`, {
      params: params
    })
  }// 获取库位

  createPosition(data: any) {
    return this.http.post(`${environment.apiUrl}/Warehouse/CreatePosition`, data)
  }// 创建库位

  deletePosition(id: number) {
    return this.http.delete(`${environment.apiUrl}/Warehouse/DeletePosition`, {
      params: { id: id }
    })
  }// 删除库位

  deletePositions(id: any) {
    const options = {
      body: id,
      responseType: 'text' as 'json'
    };
    return this.http.delete(`${environment.apiUrl}/Warehouse/DeletePositions`, options)
  }// 批量删除库位

  disablePositions(data: any) {
    return this.http.post(`${environment.apiUrl}/Warehouse/DisablePositions`, data)
  }// 禁用库位

  enablePositions(data: any) {
    return this.http.post(`${environment.apiUrl}/Warehouse/EnablePositions`, data)
  }// 启用库位

  updatePosition(id: any, data: any) {
    return this.http.put(`${environment.apiUrl}/Warehouse/UpdatePosition`, data, { params: { id } })
  }// 修改库位

  downloadPositionFileModel() {
    return this.http.get(`${environment.apiUrl}/Excel/DownloadPositionFileModel`, { responseType: 'blob' })
  }// 下载库位模板

  importPositionsFromExcel(file: File) {
    const formData = new FormData();
    formData.append('file', file);
    const url = `${environment.apiUrl}/Excel/ImportPositionsFromExcel`;
    return this.http.post(url, formData, { responseType: 'text' });
  }// 导入库位

  downloadPositionFile() {
    return this.http.get(`${environment.apiUrl}/Excel/DownloadPositionFile`, { responseType: 'blob' })
  }// 下载库位

  getPositons(WarehouseCategoryId?: number, Status?: string, SearchTerm?: string, PageNumber?: number, PageSize?: number) {
    let params = new HttpParams();
    if (WarehouseCategoryId) {
      params = params.set('WarehouseId', WarehouseCategoryId.toString());
    }
    if (Status) {
      params = params.set('Status', Status.toString());
    }
    if (SearchTerm) {
      params = params.set('SearchTerm', SearchTerm);
    }
    if (PageNumber) {
      params = params.set('PageNumber', PageNumber.toString());
    }
    if (PageSize) {
      params = params.set('PageSize', PageSize.toString());
    }
    return this.http.get(`${environment.apiUrl}/Warehouse/GetPositions`, {
      params: params
    })
  }
  CreateOtherWarehousingModel(data: any) {
    return this.http.post(`${environment.apiUrl}/Warehouse/CreateOtherWarehousingModel`, data)
  }// 创建其他入库模板
  DeleteOtherWarehousingModel(id: any) {
    return this.http.delete(`${environment.apiUrl}/Warehouse/DeleteOtherWarehousingModel?id=${id}`);
  }// 删除其他入库模板
  EditOtherWarehousingModel(id: any, data: any) {
    return this.http.put(`${environment.apiUrl}/Warehouse/UpdateOtherWarehousingModel`, data, { params: { id } })
  }// 修改其他入库模板

  GetPositionById(id: any) {
    return this.http.get(`${environment.apiUrl}/Warehouse/GetPositionById`, {
      params: { id }
    })
  }// 根据id获取库位
  GetOtherWarehousingModelsPaged(OtherWarehousingNum: string, DateTime: Date, StartTime: Date, EndTime: Date, PageIndex: number, PageSize: number) {
    let params = new HttpParams();
    if (OtherWarehousingNum) {
      params = params.set('OtherWarehousingNum', OtherWarehousingNum);
    }
    if (DateTime) {
      params = params.set('DateTime', DateTime.toString());
    }
    if (StartTime) {
      params = params.set('StartTime', StartTime.toString());
    }
    if (EndTime) {
      params = params.set('EndTime', EndTime.toString());
    }
    if (PageIndex) {
      params = params.set('PageIndex', PageIndex.toString());
    }
    if (PageSize) {
      params = params.set('PageSize', PageSize.toString());
    }
    return this.http.get(`${environment.apiUrl}/Warehouse/GetOtherWarehousingModelsPaged`, {
      params: params
    });
  }
  GetOtherWarehousingModel(id: any) {
    return this.http.get(`${environment.apiUrl}/Warehouse/GetOtherWarehousingModel`, {
      params: { id }
    });
  }

  ReceiptOtherWarehousingItems(id: any) {
    return this.http.post(`${environment.apiUrl}/Warehouse/ReceiptOtherWarehousingItems`, {}, {
      params: { id }
    });
  }

  GetIssuedPositionInventoryByPartNum(PartNum: string, PageNumber: number, PageSize: number) {
    let params = new HttpParams();
    if (PartNum) {
      params = params.set('PartNum', PartNum);
    }
    if (PageNumber) {
      params = params.set('PageNumber', PageNumber.toString());
    }
    if (PageSize) {
      params = params.set('PageSize', PageSize.toString());
    }
    return this.http.get(`${environment.apiUrl}/Warehouse/GetIssuedPositionInventory`, {
      params: params
    });
  }
  CreateJobOrderIssue(data: any) {
    return this.http.post(`${environment.apiUrl}/Warehouse/CreateJobOrderIssue`, data);
  }
  CreateJobOrderReturn(data: any) {
    return this.http.post(`${environment.apiUrl}/Warehouse/CreateJobOrderReturn`, data);
  }
  GetAllJobOrderIssue(JobOrderIssueNum: string, StartTime: string, EndTime: string, PageNumber: number, PageSize: number) {
    let params = new HttpParams();
    if (JobOrderIssueNum) {
      params = params.set('Num', JobOrderIssueNum);
    }
    if (StartTime) {
      params = params.set('StartTime', StartTime.toString());
    }
    if (EndTime) {
      params = params.set('EndTime', EndTime.toString());
    }
    if (PageNumber) {
      params = params.set('PageNumber', PageNumber.toString());
    }
    if (PageSize) {
      params = params.set('PageSize', PageSize.toString());
    }
    return this.http.get(`${environment.apiUrl}/Warehouse/GetAllJobOrderIssue`, {
      params: params
    });
  }
  DeleteJobOrderIssue(id: any) {
    return this.http.delete(`${environment.apiUrl}/Warehouse/DeleteJobOrderIssue`, {
      params: { id }
    })
  }// 删除发料单
  GetJobOrderIssueById(id: any) {
    return this.http.get(`${environment.apiUrl}/Warehouse/GetJobOrderIssueById`, {
      params: { id }
    })
  }// 根据id获取发料单
  JobOrderIssue(issueId: any) {
    return this.http.post(`${environment.apiUrl}/Warehouse/JobOrderIssue`, {}, {
      params: { issueId }
    });
  }
  GetIssueJobOrderProcessPartsByProcessId(data: any) {
    return this.http.post(`${environment.apiUrl}/Warehouse/GetIssueJobOrderProcessPartsByProcessId`, data);
  }
  DownloadOtherWarehousingFileModel() {
    return this.http.get(`${environment.apiUrl}/Excel/DownloadOtherWarehousingFileModel`, { responseType: 'blob' });
  }
  ImportOtherWarehousingFromExcel(file: File) {
    const formData = new FormData();
    formData.append('file', file);
    const url = `${environment.apiUrl}/Excel/ImportOtherWarehousingFromExcel`;
    return this.http.post(url, formData, { responseType: 'text' });
  }
  GetAllProductionWarehousingLogs(PageNumber: number, PageSize: number) {
    let params = new HttpParams();
    if (PageNumber) {
      params = params.set('PageNumber', PageNumber.toString());
    }
    if (PageSize) {
      params = params.set('PageSize', PageSize.toString());
    }
    return this.http.get(`${environment.apiUrl}/JobOrder/GetAllProductionWarehousingLogs`, {
      params: params
    });
  }

  GetLogsByProductionWarehousingId(id: any, PageNumber: number, PageSize: number) {
    let params = new HttpParams();
    if (PageNumber) {
      params = params.set('PageNumber', PageNumber.toString());
    }
    if (PageSize) {
      params = params.set('PageSize', PageSize.toString());
    }
    return this.http.get(`${environment.apiUrl}/JobOrder/GetLogsByProductionWarehousingId`, {
      params: { id }
    });
  }
  AuditOtherWarehousingModel(id: any) {
    return this.http.put(`${environment.apiUrl}/Warehouse/AuditOtherWarehousingModel`, {}, {
      params: { id }
    });
  }
  GetAllOtherWarehousingLogs(PageNumber: number, PageSize: number) {
    let params = new HttpParams();
    if (PageNumber) {
      params = params.set('PageNumber', PageNumber.toString());
    }
    if (PageSize) {
      params = params.set('PageSize', PageSize.toString());
    }
    return this.http.get(`${environment.apiUrl}/Warehouse/GetAllOtherWarehousingLogs`, {
      params: params
    });
  }
  GetLogsByOtherWarehousingId(id: any, PageNumber: number, PageSize: number) {
    let params = new HttpParams();
    if (PageNumber) {
      params = params.set('PageNumber', PageNumber.toString());
    }
    if (PageSize) {
      params = params.set('PageSize', PageSize.toString());
    }
    return this.http.get(`${environment.apiUrl}/Warehouse/GetLogsByOtherWarehousingId`, {
      params: { id }
    });
  }
  ChangeJobOrderIssueStatus(id: any, status: any) {
    return this.http.put(`${environment.apiUrl}/Warehouse/ChangeJobOrderIssueStatus`, {}, {
      params: { id, status }
    });
  }
  UnAuditJobOrderIssue(id: any) {
    return this.http.put(`${environment.apiUrl}/Warehouse/UnAuditJobOrderIssue`, {}, {
      params: { id }
    });
  }
  GetAllJobOrderIssueLogs(PageNumber: number, PageSize: number) {
    let params = new HttpParams();
    if (PageNumber) {
      params = params.set('PageNumber', PageNumber.toString());
    }
    if (PageSize) {
      params = params.set('PageSize', PageSize.toString());
    }
    return this.http.get(`${environment.apiUrl}/Warehouse/GetAllJobOrderIssueLogs`, {
      params: params
    });
  }
  GetLogsByJobOrderIssueId(id: any, PageNumber: number, PageSize: number) {
    let params = new HttpParams();
    if (PageNumber) {
      params = params.set('PageNumber', PageNumber.toString());
    }
    if (PageSize) {
      params = params.set('PageSize', PageSize.toString());
    }
    return this.http.get(`${environment.apiUrl}/Warehouse/GetLogsByJobOrderIssueId`, {
      params: { id }
    });
  }
  GetLogsByJobOrderReturnId(id: any, PageNumber: number, PageSize: number) {
    let params = new HttpParams();
    if (PageNumber) {
      params = params.set('PageNumber', PageNumber.toString());
    }
    if (PageSize) {
      params = params.set('PageSize', PageSize.toString());
    }
    return this.http.get(`${environment.apiUrl}/Warehouse/GetLogsByJobOrderReturnId`, {
      params: { id }
    });
  }
  UpdateJobOrderIssue(id: any, data: any) {
    return this.http.put(`${environment.apiUrl}/Warehouse/UpdateJobOrderIssue`, data, {
      params: { id }
    });
  }

  getIssueJobOrderProcessParts(params: {
    PartNum?: string,
    PartName?: string,
    JobOrderNum?: string,
    JobOrderStatus?: string,
    JobOrderProcessStatus?: string,
    JobOrderType?: string,
    StartTime?: string,
    EndTime?: string,
    ProcessNum?: string,
    ProcessName?: string,
    PageNumber?: number,
    PageSize?: number
  }) {
    let httpParams = new HttpParams({ fromObject: params });
    return this.http.get(`${environment.apiUrl}/Warehouse/GetIssueJobOrderProcessParts`, { params: httpParams });
  }
  GetReturnJobOrderProcessParts(params: {
    PartNum?: string,
    PartName?: string,
    JobOrderNum?: string,
    JobOrderStatus?: string,
    JobOrderProcessStatus?: string,
    JobOrderType?: string,
    StartTime?: string,
    EndTime?: string,
    ProcessNum?: string,
    ProcessName?: string,
    PageNumber?: number,
    PageSize?: number
  }) {
    let httpParams = new HttpParams({ fromObject: params });
    return this.http.get(`${environment.apiUrl}/Warehouse/GetReturnJobOrderProcessParts`, { params: httpParams });
  }
  GetIssuedPositionInventory(params: {
    PartNum?: string,
    PartName?: string,
    PartClass?: string,
    PartType?: string,
    PageNumber?: number,
    PageSize?: number
  }) {
    let httpParams = new HttpParams({ fromObject: params });
    return this.http.get(`${environment.apiUrl}/Warehouse/GetIssuedPositionInventory`, { params: httpParams });
  }
  cleanParams(params: any): any {
    const cleanedParams: any = {};
    for (const key in params) {
      if (params[key] !== undefined && params[key] !== '') {
        cleanedParams[key] = params[key];
      }
    }
    return cleanedParams;
  }
  GetInventoryDetailsPaged(params: {
    Part?: string,
    Type?: string,
    StartTime?: string,
    EndTime?: string,
    PageIndex?: number,
    PageSize?: number
  }) {
    const cleanedParams = this.cleanParams(params);
    let httpParams = new HttpParams({ fromObject: cleanedParams });
    return this.http.get(`${environment.apiUrl}/Warehouse/GetInventoryDetailsPaged`, { params: httpParams });
  }
  GetReturnJobOrderProcessPartsByProcessId(data: any) {
    return this.http.post(`${environment.apiUrl}/Warehouse/GetReturnJobOrderProcessPartsByProcessId`, data);
  }
  GetAllJobOrderReturn(JobOrderReturnNum: string, StartTime: string, EndTime: string, PageNumber: number, PageSize: number) {
    let params = new HttpParams();
    if (JobOrderReturnNum) {
      params = params.set('Num', JobOrderReturnNum);
    }
    if (StartTime) {
      params = params.set('StartTime', StartTime.toString());
    }
    if (EndTime) {
      params = params.set('EndTime', EndTime.toString());
    }
    if (PageNumber) {
      params = params.set('PageNumber', PageNumber.toString());
    }
    if (PageSize) {
      params = params.set('PageSize', PageSize.toString());
    }
    return this.http.get(`${environment.apiUrl}/Warehouse/GetAllJobOrderReturn`, {
      params: params
    });
  }
  GetAllJobOrderReturnLogs(PageNumber: number, PageSize: number) {
    let params = new HttpParams();
    if (PageNumber) {
      params = params.set('PageNumber', PageNumber.toString());
    }
    if (PageSize) {
      params = params.set('PageSize', PageSize.toString());
    }
    return this.http.get(`${environment.apiUrl}/Warehouse/GetAllJobOrderReturnLogs`, {
      params: params
    });
  }
  GetJobOrderReturnById(id: any) {
    return this.http.get(`${environment.apiUrl}/Warehouse/GetJobOrderReturnById`, {
      params: { id }
    });
  }
  UpdateJobOrderReturn(id: any, data: any) {
    return this.http.put(`${environment.apiUrl}/Warehouse/UpdateJobOrderReturn`, data, {
      params: { id }
    });
  }
  JobOrderReturn(returnId: any) {
    return this.http.post(`${environment.apiUrl}/Warehouse/JobOrderReturn`, {}, {
      params: { returnId }
    });
  }
  AuditJobOrderReturn(id: any) {
    return this.http.put(`${environment.apiUrl}/Warehouse/AuditJobOrderReturn`, {}, {
      params: { id }
    });
  }
  DeleteJobOrderReturn(id: any) {
    return this.http.delete(`${environment.apiUrl}/Warehouse/DeleteJobOrderReturn`, {
      params: { id }
    });
  }
  GetPartsAllInventory(ids:any[]){
    let params = new HttpParams();
    ids.forEach(id => {
      params = params.append('ids', id);
    });
    return this.http.get(`${environment.apiUrl}/Warehouse/GetPartsAllInventory`, {
      params: params
    });
  }
  GetWarehouseAmountData(){
    return this.http.get(`${environment.apiUrl}/Warehouse/GetWarehouseAmountData`);
  }
  GetWarehouseToDoList(){
    return this.http.get(`${environment.apiUrl}/Warehouse/GetWarehouseToDoList`);
  }
  GetWarehouseInventoryInOrOutStatistics(){
    return this.http.get(`${environment.apiUrl}/Warehouse/GetWarehouseInventoryInOrOutStatistics`);
  }
  GetWarehouseInventoryOutStatistics(){
    return this.http.get(`${environment.apiUrl}/Warehouse/GetWarehouseInventoryOutStatistics`);
  }
  GetWarehouseInventoryInStatistics(){
    return this.http.get(`${environment.apiUrl}/Warehouse/GetWarehouseInventoryInStatistics`);
  }
  UnAuditProductionWarehousing(id:any,employeeCode:any){
    return this.http.put(`${environment.apiUrl}/JobOrder/UnAuditProductionWarehousing`,{},{
      params:{id,employeeCode}
    });
  }
}
